#!/bin/bash

if [ -z "$1" ]; then
  echo "Please specify iso path"
  exit
fi

dd if="$1" bs=1 count=446 of="ubuntu.mbr"
skip=$(/sbin/fdisk -l "$1" | fgrep '.iso2 ' | awk '{print $2}')
size=$(/sbin/fdisk -l "$1" | fgrep '.iso2 ' | awk '{print $4}')
dd if="$1" bs=512 skip="$skip" count="$size" of="ubuntu.efi"

sudo apt update
sudo apt upgrade -y
sudo apt install -y syslinux squashfs-tools genisoimage dpkg-dev gfxboot-dev libfribidi-bin vim isolinux xorriso

mkdir mnt
sudo mount -o loop $1 mnt
sudo rm -rf extract-cd
mkdir extract-cd
sudo rsync --exclude=/casper/filesystem.squashfs -a mnt/ extract-cd
sudo unsquashfs mnt/casper/filesystem.squashfs
sudo rm -rf edit
sudo mv squashfs-root edit

# use aliyun sources.list. @FIXME: this step will make grub install fail
# sudo cp sources.list edit/etc/apt/
# sudo cp apt-setup edit/usr/lib/ubiquity/apt-setup/

# set Chinese locale. @FIXME: this step will make boot/grub/x86_64.efi lost and fail to boot after installed
# sudo cp locale edit/etc/default/

# log boot event
sudo cp rc.local edit/etc/

# fix md5sum error because mkisofs --boot-info-table change isolinux.bin, remove it from checksum file
# sudo cp isolinux.bin extract-cd/isolinux/

# chroot and run chroot_customize.sh
sudo mount --bind /dev/ edit/dev
sudo mount -o bind /run/ edit/run

sudo cp chroot_customize.sh edit/
#sudo cp preseed.cfg edit/
sudo chroot edit /chroot_customize.sh
sudo rm edit/chroot_customize.sh

# orca user setting (setup ekho as default TTS)
# this may not be necessary because skel already do that.
# sudo cp settings.py edit/usr/lib/python3/dist-packages/orca/settings.py

# TODO: think about how to enable screen reader in login window of lightdm

# system will copy /etc/skel/* to user home as his init settings
sudo mkdir -p edit/etc/skel/.local/share && sudo rm -rf edit/etc/skel/.local/share/orca && sudo cp -r etc_skel_.local_share_orca edit/etc/skel/.local/share/orca

# enable screen reader by default
sudo rm -rf edit/etc/skel/.config && sudo cp -r etc_skel_.config edit/etc/skel/.config

# skip timezone setting page
sudo cp ubi-timezone.py edit/usr/lib/ubiquity/plugins/ 

# disable package auto-update
sudo cp 20auto-upgrades edit/etc/apt/apt.conf.d/20auto-upgrades

sudo umount edit/dev
# reboot may be required

# build iso
sudo chmod +w extract-cd/casper/filesystem.manifest
sudo chroot edit dpkg-query -W --showformat='${Package} ${Version}\n' > filesystem.manifest
sudo mv filesystem.manifest extract-cd/casper/filesystem.manifest
sudo cp extract-cd/casper/filesystem.manifest extract-cd/casper/filesystem.manifest-desktop
#sudo sed -i '/ubiquity/d' extract-cd/casper/filesystem.manifest-desktop
sudo sed -i '/casper/d' extract-cd/casper/filesystem.manifest-desktop
sudo rm extract-cd/casper/filesystem.squashfs

sudo mksquashfs edit extract-cd/casper/filesystem.squashfs
printf $(sudo du -sx --block-size=1 edit | cut -f1) > filesystem.size
sudo mv filesystem.size extract-cd/casper/filesystem.size

# copy install custom config
sudo cp loopback.cfg extract-cd/boot/grub/
sudo cp ubuntu.seed extract-cd/preseed/

cd extract-cd
sudo rm md5sum.txt
find -type f -print0 | sudo xargs -0 md5sum | sudo tee md5sum.txt > /dev/null

sudo xorriso -as mkisofs \
  -r -V "eGuideDog Linux 0.9" -J -joliet-long -l \
  -iso-level 3 \
  -partition_offset 16 \
  --grub2-mbr "../ubuntu.mbr" \
  --mbr-force-bootable \
  -append_partition 2 0xEF "../ubuntu.efi" \
  -appended_part_as_gpt \
  -c boot.catalog \
  -b boot/grub/i386-pc/eltorito.img \
  -no-emul-boot \
  -boot-load-size 4 \
  -boot-info-table \
  --grub2-boot-info \
  -eltorito-alt-boot \
  -e '--interval:appended_partition_2:all::' \
  -no-emul-boot \
  -o ../eguidedog-linux.iso \
  .

cd ..
sudo mv eguidedog-linux.iso /media/sf_share/

echo "done."
